/* ======================================  其它方法  ====================================== */
const Index = {
	// 设置浏览器 title
	setTitle(text){ window.document.title = text },
	// 深拷贝
	copy(data){ return this.hasObject(data) ? JSON.parse(JSON.stringify(data)) : data },
	//
	deepCopy(obj){
		const deep = function(obj){
			let clone = Array.isArray(obj) ? [] : {}
			if (obj && typeof obj === 'object') {
				for (let i in obj) {
					if (Object.prototype.hasOwnProperty.call(obj,i)) {
						if (obj[i] && typeof obj[i] === 'object') {
							clone[i] = deep(obj[i])
						} else {
							clone[i] = obj[i]
						}
					}
				}
			}
			return clone
		}
		return deep(obj)
	},
	// 惰性加载文件
	async lazy(file){ return file().then( f => f.default ) },
	// 返回值
	getValue(v){ return this.isFunction(v) ? v(v) : v },
	// 发布订阅
	proxy:{
		// 监听/订阅刷新
		on(back, name='back'){
			window.proxy[name] = back?.()
		},
		// 发布
		publish: name => window.proxy[name](),
		// 移除
		remove: (name='back') =>{
			if(window.proxy){
				window.proxy[name] = null
			}
		}
	},
	// 全局监听开关变量
	onHideShow(className, show, callback){
		document.addEventListener('mousedown', e=>{
			if(this.isString(e.target.className) && e.target.className.includes(className)){
				show = !show
			}else{
				show = false
			}
			callback?.(show)
		})
	},
	// 判断不为 false 或 0
	isNoFalse: value => value !== false || value !== 0,
	// 将字符串转为数据
	strToNum(value){ return this.isString(value) ? (isNaN(value*1) ? value : value * 1) : value},
	// 清除数组对象的重复值
	wipe: (arr,key='id') => {
		let result = []
		let obj = {}
		arr.forEach(function(v){
			if(!obj[v[key]]){
				result.push(v)
				obj[v[key]] = true
			}
		})
		return result
	},
	// 图片解析
	url: src => {
		let rs = ''
		if( src ){
			if( src.includes('://') || src.includes(';base64,')){
				rs = src
			}else {
				rs = window.$config.host + ( src.includes('upload') ? '' : '/upload/' ) + src
			}
		}
		return rs
	},
	getCurrentUrl: () => {
		let hash = window.location.hash
		return hash ? hash.replace('#', '') : window.location.pathname
	},
	// 前端自动编号 01
	getStrNo: (config, { update, add, formRef, tableRef }) => {
		if( add ){
			const d = tableRef.data()
			const len = d.length
			let no = len === 0 ? 0 : d[len -1].value
			no = +no < 9 ? ('0' + (no*1 + 1)) : +no + 1
			
			formRef.setValue({ value: no  })
		}
		window.$fn.getRow(config, 'value').disabled = !add
		update()
	},
	getIntNo: (config, { update, add, formRef, tableRef }, name='no') => {
		if( add ){
			const d = tableRef.data()
			const len = d.length
			const last = len === 0 ? 0 : d[len -1][name]
			formRef.setValue({ [name]: last + 1  })
		}
		window.$fn.getRow(config, name).disabled = !add
		update()
	},
	getIntValue: (config, d) => window.$fn.getIntNo(config, d, 'value')
}

export default Index